<template>
	<div>
		<p>
		<button class="btn btn-white btn-default btn-round" @click="refresh()">
			<i class="ace-icon fa fa-refresh red"></i>
			刷新
		</button>
		</p>

		<table id="simple-table" class="table  table-bordered table-hover">
		<thead>
			<tr>
				<th class="center">ID</th>
				<th class="center">相对路径</th>
				<th class="center">文件名</th>
				<th class="center">后缀</th>
				<th class="center">大小</th>
				<th class="center">用途</th>
				<th class="center">操作</th>
			</tr>
		</thead>

		<tbody>
			<tr v-for="file in files">
				<td class="center">{{file.id}}</td>
				<td class="center">{{file.path}}</td>
				<td class="center">{{file.name}}</td>
				<td class="center">{{file.suffix}}</td>
				<td class="center">{{file.size | formatFileSize}}</td>
				<td class="center">{{FILE_USE | optionKVObj(file.use)}}</td>
				<td class="center" style="width:100px;">
					<div class=" btn-group">&nbsp;
						<button v-on:click="deleteFile(file)" class="btn btn-white btn-xs btn-danger btn-round">
							<i class="ace-icon fa fa-remove bigger-120"></i>
						</button>
					</div>

				</td>
			</tr>
			<tr v-if="files.length === 0">
				<td colspan="7" class="center" style="background-color: lightgrey;font-family: 'Courier New',serif">暂无数据</td>
			</tr>
	</tbody>
	</table>
	<!-- 分页组件 -->
	<pagination ref="pagination" v-bind:list="list"></pagination>
	</div>

</template>

<script>
	import Pagination from "../../components/pagination.vue"
	export default {
		name: "file",
		components:{
			Pagination
		},
		data:function(){
			return{
				files:[],
				FILE_USE:FILE_USE
			}
		},
		mounted: function(){
			this.$parent.activeSidebar("file-file-sidebar");
			let _this = this;
			_this.list();
		},
		methods:{

			refresh() {
				let _this = this;
				Loading.show();
				_this.list(_this.$refs.pagination.page);
				Loading.hide();
			},
			/* 查找文件 */
			list(page=1) {
				let _this = this;
				_this.$ajax.post(process.env.VUE_APP_SERVER + '/file/admin/file/list',{
					page:page,
					size:_this.$refs.pagination.size
				}).then((response)=>{
					_this.files = response.data.data.list;
					_this.$refs.pagination.render(page, response.data.data.total);
				})
			},
			/*删除文件*/
			deleteFile(file){
				console.log("file:",file);
				let _this = this;
				let data = file;
				Confirm.show("",()=>{
					Loading.show();
					_this.$ajax.post(process.env.VUE_APP_SERVER + '/file/admin/file/delete',data).then((response)=>{
						if (response.data.success){
							Toast.success(response.data.message);
						} else {
							Toast.error(response.data.message);
						}
						_this.list(_this.$refs.pagination.page);
						Loading.hide();
					})
				});

			}
		}
	}
</script>
